﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CQIE.TES.IDAL;
using CQIE.TES.Model;

namespace CQIE.TES.DAL
{
    public class CourseDAL: DALBase<Course>,ICourseDAL
    {
        public List<Course> GetCoursesInfo(int pageNum, int pageSize, string search, out int count)
        {
            using (MyDB db = new MyDB())
            {
                count = db.Course.Count(c => c.Name.Contains(search)&& c.IsAlive==true);
                // 模糊查询 Contains()
                // Skip():是跳过前n个结果，返回剩余的结果
                // Take():作用是从查询结果中提取前n个结果
                var query = (from t in db.Course
                    where t.Name.Contains(search) && t.IsAlive==true
                    orderby t.ID
                    select t).Skip(pageNum * pageSize).Take(pageSize).ToList();

                return query;
            }
        }

        [Obsolete("方法已经废弃，使用GetCoursesInfo()")]
        public List<Course> GetCoursesInfo(string search)
        {
            return null;
        }

        public bool Delete(int id)
        {
            using (MyDB db = new MyDB())
            {
                try
                {
                    var model = db.Course.Find(id);
                    model.IsAlive = false;
                    db.SaveChanges();

                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }

        public List<Teacher> CourserTeachers(int courseID)
        {
            using (MyDB db = new MyDB())
            {
                var course = db.Course.Find(courseID);

                course.Teachers = course.Teacher_Course.Select(r => new Teacher()
                {
                    ID = r.Teacher.ID,
                    Name = r.Teacher.Name,
                    WorkNumber = r.Teacher.WorkNumber,
                    IsFreeze = r.Teacher.IsFreeze
                }).ToList();

                course.Teachers = course.Teachers.Where(r => r.IsFreeze == false).ToList();

                return course.Teachers;
            }
        }

        
        public void GiveOrRemoveCourseTeacher(int courseId, List<int> teacherIds,int type)
        {
            using (MyDB db = new MyDB())    
            {
                var model = db.Course.Find(courseId);
                if (type == 0)
                {
                    // 移除
                    foreach (int tid in teacherIds)
                    {
                        // 需要移除的老师
                        var temp = model.Teacher_Course.Where(r => r.Teacher.ID == tid).ToList();

                        for (int i = 0; i < temp.Count(); i++)
                        {
                            model.Teacher_Course.Remove(temp[i]);
                        }
                    }
                }
                else
                {
                    // 添加
                    foreach (int tid in teacherIds)
                    {
                        

                        model.Teacher_Course.Add(new Teacher_Course()
                        {
                            Course = model,
                            Teacher = db.Teacher.Find(tid)
                        });
                    }
                }
                db.SaveChanges();
            }
        }

        /// <summary>
        /// 获取所有没被冻结的课程
        /// </summary>
        /// <returns></returns>
        public List<Course> GetAllCourse() {

            using (MyDB db = new MyDB()) {
                var allCourse = (from c in db.Course
                                 where c.IsAlive == false
                                 select c).ToList();

                return allCourse;
            }
        }

        public List<Course> GetClassCourse(int cid, int studyYearId, int pageNum, out int count, int limit)
        {
            using (MyDB db = new MyDB())
            {
                var query = db.Class_Course.Where(r => r.ClassID == cid && r.StudyYearID == studyYearId).OrderBy(r=>r.ID).Skip(limit*pageNum).Take(limit).ToList();
                count = query.Count;
     
                return query.Select(r => new Course()
                {
                    ID = r.CourseInfo.Course.ID,
                    Name = r.CourseInfo.Course.Name,
                    Teacher = r.CourseInfo.Teacher
                }).ToList();
            }
        }

        public void test()
        {
            using (MyDB db = new MyDB())
            {
                var c = db.Class_Course.ToList();
            }
        }

        [Obsolete]
        public List<Course> GetClassCourse(int cid, int evaId)
        {
            using (MyDB db = new MyDB())
            {
                var eva = db.EvaluaHeight.Find(evaId);
                var query = db.Class_Course.Where(r => r.ClassID == cid && r.StudyYearID == eva.StudyYearId).ToList();
                return query.Select(r => new Course()
                {
                    ID = r.CourseInfo.Course.ID,
                    Name = r.CourseInfo.Course.Name,
                    Teacher = r.CourseInfo.Teacher
                }).ToList();
            }
        }
        /// <summary>
        /// 添加课程
        /// </summary>
        /// <param name="name">课程名</param>
        /// <param name="courseCode">课程号</param>
        /// <param name="isAlive">默认启用课程</param>
        public void InsertCourse(string name, string courseCode, bool isAlive)
        {
            using (MyDB db = new MyDB()) {
                db.Course.Add(new Course()
                {
                    Name = name,
                    CourseCode = courseCode,
                    IsAlive = isAlive
                });
                db.SaveChanges();
            }
        }

        public List<Course> GetClassHaveNoCourse(int classId, int studyYear)
        {
            using (MyDB db = new MyDB())
            {
                var c = db.Class_Course.Where(r => r.ClassID == classId && r.StudyYearID ==studyYear).ToList();
                var allCourse = db.Course.Where(r=>r.IsAlive==true).ToList();
                c.ForEach(r => { allCourse = allCourse.Where(ac => ac.ID != r.ClassID).ToList(); });

                return allCourse;
            }
        }

        public bool ClassAddCourse(int classId, int studyYear, int courseId, int teacherId)
        {
            using (MyDB db = new MyDB())
            {
                var courseInfo = db.Teacher_Course.FirstOrDefault(r => r.CourseID == courseId && r.TeacherID == teacherId);
                Class_Course model = new Class_Course();
                model.ClassID = classId;
                model.StudyYearID = studyYear;
                model.CourseInfo = courseInfo;
                db.Class_Course.Add(model);
                if (db.SaveChanges()>0)
                {
                    return true;
                }

                return false;
            }
        }

        public List<Teacher_Course> GetTeacherCourse(int teacherId)
        {
            using (MyDB db = new MyDB())
            {
                return db.Teacher_Course.Include(r => r.Course).Where(r => r.TeacherID == teacherId).ToList();
            }
        }
    }
}
